package com.hkts.naturalgas.flow.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.flow.VO.FlowNodeinfoVo;
import com.hkts.naturalgas.flow.entity.FlowNodeHandler;
import com.hkts.naturalgas.flow.entity.FlowNodeinfo;
import com.hkts.naturalgas.flow.entity.FlowTaskHandler;
import com.hkts.naturalgas.flow.entity.FlowTaskInstance;
import com.hkts.naturalgas.flow.mapper.FlowTaskHandlerMapper;
import com.hkts.naturalgas.flow.service.IFlowNodeHandlerService;
import com.hkts.naturalgas.flow.service.IFlowNodeinfoService;
import com.hkts.naturalgas.flow.service.IFlowTaskHandlerService;
import com.hkts.naturalgas.organizational.mapper.HontUserMapper;
import com.hkts.naturalgas.organizational.orgVo.HontUserRolelevelQueryVo;
import com.hkts.naturalgas.organizational.orgVo.HontUserRolelevelVo;
import com.hkts.naturalgas.performance.performanceTo.HontUserTo;
import com.hkts.naturalgas.utils.BeanConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-09-07
 */
@Service
@Transactional
@Slf4j
public class FlowTaskHandlerServiceImpl extends ServiceImpl<FlowTaskHandlerMapper, FlowTaskHandler> implements IFlowTaskHandlerService {
    @Autowired
    private FlowTaskHandlerMapper flowTaskHandlerMapper;
    @Autowired
    private IFlowNodeHandlerService iFlowNodeHandlerService;
    @Autowired
    private IFlowNodeinfoService iFlowNodeinfoService;
    @Autowired
    private HontUserMapper hontUserMapper;

    public FlowNodeinfoVo getFlowNodeinfoOnLevel(FlowNodeinfo flowNodeinfo, Integer level){
        //根据节点查询等级大于等于此用户的用户
        List<FlowNodeHandler> flowNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowNodeinfo.getId(),level);
        if(flowNodeHandlers.size()==0){
            if(FlowNodeinfo.NODE_TYPE_FINISH.equals(flowNodeinfo.getNodeType())){
                return null;
            }
            FlowNodeinfo flowNodeinfo1=
                    iFlowNodeinfoService.getFlowNodeinfo(flowNodeinfo.getTemplateId(),flowNodeinfo.getTemplateVersion(),flowNodeinfo.getSetpId()+1);
            return getFlowNodeinfoOnLevel(flowNodeinfo1,level);
        }
        FlowNodeinfoVo flowNodeinfoVo= BeanConvertUtils.copy(flowNodeinfo,FlowNodeinfoVo.class);
        flowNodeinfoVo.setFlowNodeHandlers(flowNodeHandlers);
        return flowNodeinfoVo;
    }
    @Override
    public FlowNodeinfoVo addFlowTaskHandlerOnLevel(FlowNodeinfo flowNodeinfo, Integer level,
                                     String hontDepartmentId, String taskId,Integer isOrganization) {
        List<FlowTaskHandler> flowTaskHandlers=new ArrayList<>();
        //根据节点查询等级大于等于此用户的用户
//        List<FlowNodeHandler> flowNodeHandlers=
//                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowNodeinfo.getId(),level);
//        if(flowNodeHandlers.size()==0){
//            if(FlowNodeinfo.NODE_TYPE_FINISH.equals(flowNodeinfo.getNodeType())){
//                return null;
//            }
//            FlowNodeinfo flowNodeinfo1=
//                    iFlowNodeinfoService.getFlowNodeinfo(flowNodeinfo.getTemplateId(),flowNodeinfo.getSetpId()+1);
//            return addFlowTaskHandlerOnLevel(flowNodeinfo1,level,hontDepartmentId,taskId);
//        }
        FlowNodeinfoVo flowNodeinfoVo=getFlowNodeinfoOnLevel(flowNodeinfo,level);
        log.info("跳过低角色登记获取节点数据{}",flowNodeinfoVo);
        if(flowNodeinfoVo==null){
            return flowNodeinfoVo;
        }
        List<FlowNodeHandler> flowNodeHandlers=flowNodeinfoVo.getFlowNodeHandlers();
        if(flowNodeinfoVo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){
            //添加用户审批人
//            List<FlowNodeHandler> userNodeHandlers=
//                    flowNodeHandlers.stream().filter(t->hontDepartmentId.contains(t.getHontDepartmentId())).collect(Collectors.toList());
//            if(userNodeHandlers.size()==0){
//                return null;
//            }
            for (FlowNodeHandler item:flowNodeHandlers){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getNodeHandlerId());
                flowTaskHandler.setHontUserName(item.getNodeHandler());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }else {
            //添加角色审批人
            List<String> rloeids=flowNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());
            HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
            hontUserRolelevelQueryVo.setRoleIds(rloeids);
            if(isOrganization==1){
                hontUserRolelevelQueryVo.setDepartmentId(hontDepartmentId.substring(0,2));
            }
            List<HontUserRolelevelVo> userRolelevelVos=
                    hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
            for (HontUserRolelevelVo item:userRolelevelVos){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getId());
                flowTaskHandler.setHontUserName(item.getNickName());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }
        flowTaskHandlerMapper.insertBatch(flowTaskHandlers);
        return flowNodeinfoVo;

    }

    @Override
    public FlowNodeinfo addFlowTaskHandler(FlowNodeinfo flowNodeinfo, String hontDepartmentId,
                                           String taskId,Integer isOrganization) {
        List<FlowTaskHandler> flowTaskHandlers=new ArrayList<>();
        List<FlowNodeHandler> flowNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowNodeinfo.getId(),null);
        log.info("添加事件审批人{}",flowNodeHandlers);
        if(flowNodeHandlers.size()==0){
            return null;
        }
        if(flowNodeinfo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){
            //添加用户审批人

            if(flowNodeHandlers.size()==0){
                return null;
            }
            for (FlowNodeHandler item:flowNodeHandlers){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getNodeHandlerId());
                flowTaskHandler.setHontUserName(item.getNodeHandler());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }else {
            //添加角色审批人
            List<String> rloeids=flowNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());
            HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
            hontUserRolelevelQueryVo.setRoleIds(rloeids);
            if(isOrganization==1){
                hontUserRolelevelQueryVo.setDepartmentId(hontDepartmentId.substring(0,2));
            }
            List<HontUserRolelevelVo> userRolelevelVos=
                    hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
            for (HontUserRolelevelVo item:userRolelevelVos){
                FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                flowTaskHandler.setTaskId(taskId);
                flowTaskHandler.setHontUserId(item.getId());
                flowTaskHandler.setHontUserName(item.getNickName());
                flowTaskHandlers.add(flowTaskHandler);
            }
        }
        flowTaskHandlerMapper.insertBatch(flowTaskHandlers);
        return flowNodeinfo;
    }

    @Override
    public boolean checkUser(String taskId, String userId) {
        QueryWrapper<FlowTaskHandler> query = Wrappers.query();
        query.lambda().eq(FlowTaskHandler::getTaskId,taskId)
                .eq(FlowTaskHandler::getHontUserId,userId);
        int count=flowTaskHandlerMapper.selectCount(query);
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public void deleteByTaskId(String taskId) {
        QueryWrapper<FlowTaskHandler> query = Wrappers.query();
        query.lambda().eq(FlowTaskHandler::getTaskId,taskId);
        flowTaskHandlerMapper.delete(query);
    }
    public boolean skipNodeInfo(HontUserTo u,FlowNodeinfo flowNodeinfo, String hontDepartmentId,Integer isOrganization){
        List<FlowNodeHandler> userNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowNodeinfo.getId(),null);
        log.info("是否跳过此节点,当前用户{},节点审批数据{}",u,userNodeHandlers);
        if(flowNodeinfo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){
            if(userNodeHandlers.size()==1&&userNodeHandlers.get(0).getNodeHandlerId().equals(u.getId())){
                return true;
            }
        }else {
            List<String> rloeids=userNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());

            HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
            hontUserRolelevelQueryVo.setRoleIds(rloeids);
            hontUserRolelevelQueryVo.setUserId(u.getId());
            if(isOrganization==1){
                String departStr=hontDepartmentId.substring(0,2);
                hontUserRolelevelQueryVo.setDepartmentId(departStr);
            }
            List<HontUserRolelevelVo> userRolelevelVos=
                    hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
            if(userRolelevelVos.size()>0&&rloeids.size()==1){
                return true;
            }

        }
        return false;
    }
    @Override
    public void updateFlowTaskHandler(FlowTaskInstance flowTaskInstance, String hontDepartmentId,Integer isOrganization) {
        deleteByTaskId(flowTaskInstance.getId());

        List<FlowTaskHandler> flowTaskHandlers=new ArrayList<>();
        FlowNodeinfo flowNodeinfo=iFlowNodeinfoService.getById(flowTaskInstance.getFlowNodeId());
        List<FlowNodeHandler> userNodeHandlers=
                iFlowNodeHandlerService.getFlowNodeHandlerOnUser(flowTaskInstance.getFlowNodeId(),null);
        log.info("事件审批人添加当前节点人数据{}",userNodeHandlers);
       if(flowNodeinfo.getApprovalType()==FlowNodeinfo.NODEHANDLER_TYPE_USER){
//            if(isOrganization==1){
//                userNodeHandlers=
//                       userNodeHandlers.stream().filter(t->hontDepartmentId.contains(t.getHontDepartmentId())).collect(Collectors.toList());
//               if(userNodeHandlers.size()==0){
//                   return ;
//               }
//            }
           for (FlowNodeHandler item:userNodeHandlers){
               FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
               flowTaskHandler.setTaskId(flowTaskInstance.getId());
               flowTaskHandler.setHontUserId(item.getNodeHandlerId());
               flowTaskHandler.setHontUserName(item.getNodeHandler());
               flowTaskHandlers.add(flowTaskHandler);
           }
       }else {
           List<String> rloeids=userNodeHandlers.stream().map(t->t.getNodeHandlerId()).collect(Collectors.toList());
           HontUserRolelevelQueryVo hontUserRolelevelQueryVo=new HontUserRolelevelQueryVo();
           hontUserRolelevelQueryVo.setRoleIds(rloeids);
           if(isOrganization==1){
               String departStr=hontDepartmentId.substring(0,2);
               hontUserRolelevelQueryVo.setDepartmentId(departStr);
           }
           List<HontUserRolelevelVo> userRolelevelVos=
                   hontUserMapper.getHontUserAndRoleAndLevel(hontUserRolelevelQueryVo);
           if(userRolelevelVos.size()>0){
               for (HontUserRolelevelVo item:userRolelevelVos){
                   FlowTaskHandler flowTaskHandler=new FlowTaskHandler();
                   flowTaskHandler.setTaskId(flowTaskInstance.getId());
                   flowTaskHandler.setHontUserId(item.getId());
                   flowTaskHandler.setHontUserName(item.getNickName());
                   flowTaskHandlers.add(flowTaskHandler);
               }
               //TODO 抛异常，无下个审批节点

           }

       }
        flowTaskHandlerMapper.insertBatch(flowTaskHandlers);

    }
}
